# -*- coding: utf-8 -*-
import sys
import pygame
from settings import Settings
from game_stats import GameStats
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from alien import Alien
from button import Button
from button import Button2
from scoreboard import Scoreboard
# from 外星人入侵.guanshu import Window

pygame.mixer.init()
# pygame.mixer.music.load("musics/bgmusic1.mp3")
  # 背景音乐


class start:
    pygame.mixer.music.load("musics/1.wav")
    pygame.mixer.music.set_volume(0.8)
    def run_game(self):
        pygame.mixer.music.load("musics/1.wav")
        pygame.mixer.music.set_volume(0.8)
        #初始化游戏
        pygame.init()
        #创建一个屏幕窗口
        ai_settings = Settings()
        screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
        #设置窗口标题
        pygame.display.set_caption("Alien Invasion")
        #创建Play按钮
        play_button_text = "Fight alongside the Fourth Squad"
        play_button = Button(ai_settings, screen, play_button_text)

        play2_button = Button2(ai_settings,screen,"Two_Players")

        pygame.mixer.music.play(-1)
        # 创建一个用于存储游戏统计信息的案例,并创建记分牌
        stats = GameStats(ai_settings)

        sb = Scoreboard(ai_settings, screen, stats)
        # 创建一艘飞船、一个子弹编组和一个外星人编组
        ship = Ship(ai_settings, screen)
        bullets = Group()
        aliens = Group()
        # 创建外星人群
        gf.create_fleet(ai_settings,screen,ship ,aliens)
        # 设置背景颜色
        # 创建一个外星人
        alien = Alien(ai_settings, screen)
        f1 = gf.flag()
        t2 = False
        # 开始游戏的主循环
        while True:
            if f1.recharge_success2 and t2 == False :
              t2 = True
              pygame.mixer.music.load("musics/bgmusic1.mp3")
              pygame.mixer.music.play(-1)
              print("bug")
              ship.image = pygame.image.load('images/ship3.png')
            # 主循环检查玩家的输入
            gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets,play2_button)
            if stats.game_active:
                #更新飞船的位置
                ship.update()
                #所有未消失的子弹的位置
                gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
                #更新外星人的位置
                gf.update_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets)
            #更新后的位置来绘制新屏幕
            gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button, play2_button)
